package user

import (
	user_serve "hft/Http/Services/user"
	"hft/Http/Services/user/user_model"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type User struct{}

func (*User) List(c *gin.Context) {

	var paginate *user_model.UserSearchParams
	c.ShouldBindJSON(&paginate)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
	}
	list, _ := user_serve.UserService.List(paginate, claims.BaseClaims)

	utils.OkWithData(list, c)
}

func (*User) Detail(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	if id == 0 {
		utils.FailWithMessage(e.WrongParams, c)
	}
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage(e.InValidToken, c)
	} else {
		if data, err := user_serve.UserService.Detail(claims.BaseClaims, id); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithData(data, c)
		}
	}
}

// 屏蔽电话
func (*User) ShieldPhone(c *gin.Context) {
	var data *user_model.ShieldPhone
	c.ShouldBindJSON(&data)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	if err := user_serve.UserService.ShieldPhone(int(data.ID), claims.BaseClaims, data.IsShield); err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)

}

var NewUserController = new(User)
